home *** CD-ROM | disk | FTP | other *** search
- import java.io.IOException;
- import java.util.Random;
- import javax.microedition.lcdui.Canvas;
- import javax.microedition.lcdui.Font;
- import javax.microedition.lcdui.Graphics;
- import javax.microedition.lcdui.Image;
-
- public class BlocksCanvas extends Canvas implements Runnable {
- static final int MINIMUMSCROLLDELAY = 150;
- static int level = 1;
- Random random = new Random();
- private int initTime;
- private int timeleft;
- private int extraTime = 0;
- private int elapsedTime = 0;
- private int scores = 0;
- private Blocks parent;
- private boolean gameOver = false;
- private static final int ROWS = 7;
- private static final int COLS = 7;
- private static final int MIDDLE = 3;
- private int[][] board = new int[7][7];
- private int width;
- private int height;
- private int p1top;
- private int p2top;
- private int pleft;
- private int p1height;
- private int bsize = 20;
- private int GAP;
- private int cellW;
- private int cellH;
- private int bgcolor = 255;
- private int blockcolor = 127;
- private int pencolor = 0;
- // $FF: renamed from: bg javax.microedition.lcdui.Graphics
- Graphics field_0;
- Image buf;
- Font fcal;
- int state;
- int fontH;
- int fontW;
- int fontW2;
- int waitblock = 0;
- int direct = 0;
- int mblock_x;
- int mblock_y;
- static int count = 0;
- int remains;
- int fallen;
- private static final int maxblocks = 6;
- Image[] imgs = new Image[6];
- boolean[] left = new boolean[6];
- Thread runner;
-
- public BlocksCanvas(Blocks creator) {
- this.parent = creator;
- this.height = ((Canvas)this).getHeight();
- this.width = ((Canvas)this).getWidth();
- this.buf = Image.createImage(this.width, this.height);
- this.field_0 = this.buf.getGraphics();
- this.fcal = Font.getFont(0, 0, 8);
- this.fontH = this.fcal.getHeight();
- this.fontW2 = this.fcal.stringWidth(" Game Over ");
-
- try {
- if (this.width < 120) {
- this.imgs[0] = Image.createImage("/pics/a10.png");
- this.imgs[1] = Image.createImage("/pics/b10.png");
- this.imgs[2] = Image.createImage("/pics/c10.png");
- this.imgs[3] = Image.createImage("/pics/j10.png");
- this.imgs[4] = Image.createImage("/pics/k10.png");
- this.imgs[5] = Image.createImage("/pics/n10.png");
- this.bsize = this.cellW = 10;
- this.p1height = this.cellW + 4;
- this.GAP = 1;
- } else {
- this.imgs[0] = Image.createImage("/pics/a20.png");
- this.imgs[1] = Image.createImage("/pics/b20.png");
- this.imgs[2] = Image.createImage("/pics/c20.png");
- this.imgs[3] = Image.createImage("/pics/j20.png");
- this.imgs[4] = Image.createImage("/pics/k20.png");
- this.imgs[5] = Image.createImage("/pics/n20.png");
- this.bsize = this.cellW = 20;
- this.p1height = this.cellW + 4;
- this.GAP = 3;
- }
- } catch (IOException var3) {
- }
-
- this.p1top = 0;
- this.p2top = this.p1top + this.p1height + 2 * this.GAP;
- this.pleft = (this.width - this.bsize * 7 - 6 * this.GAP) / 2;
- this.reset();
- }
-
- public void reset() {
- this.initTime = 99000 - (level - 1) * 2000;
- if (this.initTime < 89000) {
- this.initTime = 80000;
- }
-
- this.timeleft = this.initTime;
- this.state = 1;
- this.direct = 0;
-
- for(int i = 0; i < 6; ++i) {
- this.left[i] = false;
- }
-
- this.initBoard();
- this.mblock_x = 3;
- this.mblock_y = 0;
- }
-
- public void initBoard() {
- for(int i = 0; i < 7; ++i) {
- for(int j = 0; j < 7; ++j) {
- if (i <= 2) {
- this.board[i][j] = 0;
- } else {
- boolean cont = true;
-
- while(cont) {
- cont = false;
- this.board[i][j] = (this.random.nextInt() >>> 1) % 6 + 1;
- if (j != 0) {
- if (this.board[i][j] == this.board[i][j - 1] || this.board[i][j] == this.board[i - 1][j]) {
- cont = true;
- }
- } else if (this.board[i][j] == this.board[i - 1][j]) {
- cont = true;
- }
- }
- }
- }
- }
-
- this.board[0][3] = (this.random.nextInt() >>> 1) % 6 + 1;
- this.waitblock = (this.random.nextInt() >>> 1) % 6 + 1;
- this.field_0.setGrayScale(this.bgcolor);
- this.field_0.fillRect(0, 0, this.width, this.height);
-
- for(int i = 0; i < 7; ++i) {
- for(int j = 0; j < 7; ++j) {
- if (this.board[i][j] != 0) {
- int x = this.getLeft(j);
- int y = this.getTop(i);
- int ii = this.board[i][j] - 1;
- this.field_0.drawImage(this.imgs[ii], x, y, 16 | 4);
- }
- }
- }
-
- this.drawWaitblock(this.waitblock);
- this.remains = 28;
- this.fallen = 0;
- }
-
- private int getTop(int x) {
- int i = 0;
- i = this.p2top + this.bsize * x + x * this.GAP;
- return i;
- }
-
- private int getLeft(int x) {
- int i = 0;
- if (x > 0) {
- i = this.pleft + this.bsize * x + x * this.GAP;
- } else {
- i = this.pleft + this.bsize * x;
- }
-
- return i;
- }
-
- public void run() {
- long nextTime = 0L;
- long stopTime = 0L;
- int minus_time = 0;
- boolean trigger = true;
- boolean xswitch = true;
- if (this.gameOver) {
- this.field_0.drawString("GAME OVER", this.width / 2, this.height / 2, 16 | 1);
- } else {
- this.paintTime();
- }
-
- for(; !this.gameOver; ((Canvas)this).repaint()) {
- long timeNow = System.currentTimeMillis();
- if (trigger) {
- stopTime = timeNow;
- trigger = false;
- }
-
- this.elapsedTime += 150;
- this.timeleft = this.initTime - this.elapsedTime + this.extraTime;
- if (nextTime < timeNow + 150L) {
- nextTime = timeNow + 150L;
- }
-
- try {
- Thread.sleep(nextTime - timeNow);
- } catch (Exception var20) {
- }
-
- nextTime = System.currentTimeMillis() + 150L;
- switch (this.state) {
- case 1:
- this.paintTime();
- minus_time = Math.min(1000, this.fallen * 4);
- if (this.direct != 2 && nextTime - 150L - stopTime > (long)(4000 - minus_time)) {
- if (xswitch) {
- int x = this.getLeft(this.mblock_x);
- int y = this.getTop(0);
- this.field_0.setGrayScale(this.bgcolor);
- this.field_0.fillRect(x, y, this.bsize, this.bsize);
- xswitch = false;
- } else {
- int var21 = this.getLeft(this.mblock_x);
- int var23 = this.getTop(0);
- int ii = this.board[0][this.mblock_x] - 1;
- this.field_0.drawImage(this.imgs[ii], var21, var23, 16 | 4);
- xswitch = true;
- }
-
- minus_time += Math.min(1000, level * 150);
- if (nextTime - 150L - stopTime > (long)(7500 - minus_time)) {
- stopTime = nextTime;
- this.direct = 2;
- }
- }
-
- if (this.direct != 0) {
- if (this.direct == 1) {
- int temp_x = this.mblock_x - 1;
- if (temp_x <= -1) {
- temp_x = 6;
- }
-
- this.moveBlock(this.mblock_x, 0, temp_x, 0, this.board[0][this.mblock_x]);
- this.mblock_x = temp_x;
- this.direct = 0;
- } else if (this.direct == 2) {
- stopTime = 0L;
- int temp_y = this.mblock_y + 1;
- if (temp_y <= 6 && this.board[temp_y][this.mblock_x] == 0) {
- this.moveBlock(this.mblock_x, this.mblock_y, this.mblock_x, temp_y, this.board[this.mblock_y][this.mblock_x]);
- this.mblock_y = temp_y;
- } else {
- ++this.remains;
- ++this.fallen;
- this.scores += 10;
- this.findconnect(this.mblock_x, this.mblock_y, this.board[this.mblock_y][this.mblock_x]);
- if (count >= 2) {
- this.remove();
- this.paintTime();
- this.rearrange();
- this.reverse();
- this.extraTime += (count - 1) * 2000;
- this.scores += (count - 1) * 20;
-
- for(int i = 6; i >= 1; --i) {
- for(int j = 0; j < 7; ++j) {
- count = 0;
- if (this.board[i][j] != 0) {
- this.findconnect(j, i, this.board[i][j]);
- if (count >= 2) {
- this.remove();
- this.rearrange();
- this.reverse();
- this.extraTime += (count - 1) * 2000;
- this.scores += (count - 1) * 20;
- count = 0;
- break;
- }
-
- this.reverse();
- }
- }
- }
- }
-
- this.reverse();
- this.paintTime();
- if (!this.isGameover()) {
- if (this.fallen % 32 == 0) {
- this.raiseBlocks();
- }
-
- for(int i = 0; i < 7; ++i) {
- if (this.board[0][i] != 0) {
- this.state = 4;
- }
- }
-
- if (this.state != 4) {
- int temp = (this.random.nextInt() >>> 1) % 6 + 1;
- if (this.remains < 15) {
- for(int i = 0; i < 6; ++i) {
- this.left[i] = false;
- }
-
- for(int i = 1; i < 7; ++i) {
- for(int j = 0; j < 7; ++j) {
- if (this.board[i][j] == 1) {
- this.left[0] = true;
- } else if (this.board[i][j] == 2) {
- this.left[1] = true;
- } else if (this.board[i][j] == 3) {
- this.left[2] = true;
- } else if (this.board[i][j] == 4) {
- this.left[3] = true;
- } else if (this.board[i][j] == 5) {
- this.left[4] = true;
- } else if (this.board[i][j] == 6) {
- this.left[5] = true;
- }
- }
- }
-
- if (this.left[0] || this.left[1] || this.left[2] || this.left[3] || this.left[4] || this.left[5]) {
- while(!this.left[temp - 1]) {
- temp = (this.random.nextInt() >>> 1) % 6 + 1;
- }
- }
- }
-
- this.board[0][3] = this.waitblock;
- int var22 = this.getLeft(3);
- int var24 = this.getTop(0);
- int var26 = this.board[0][3] - 1;
- this.field_0.drawImage(this.imgs[var26], var22, var24, 16 | 4);
- this.mblock_x = 3;
- this.mblock_y = 0;
- this.waitblock = temp;
- this.drawWaitblock(temp);
- }
- }
-
- count = 0;
- this.direct = 0;
- trigger = true;
- }
- } else if (this.direct == 3) {
- int var25 = this.mblock_x + 1;
- if (var25 >= 7) {
- var25 = 0;
- }
-
- this.moveBlock(this.mblock_x, 0, var25, 0, this.board[0][this.mblock_x]);
- this.mblock_x = var25;
- this.direct = 0;
- }
- }
-
- if (this.timeleft <= 900) {
- this.state = 4;
- }
-
- if (this.remains == 0) {
- this.state = 5;
- }
- case 2:
- case 3:
- default:
- break;
- case 4:
- this.gameover();
- this.endGame();
- break;
- case 5:
- this.youwin();
- this.endGame();
- }
- }
-
- }
-
- public void paint(Graphics g) {
- g.drawImage(this.buf, 0, 0, 16 | 4);
- }
-
- private void paintTime() {
- int temp = this.timeleft / 1000;
- this.field_0.setGrayScale(this.bgcolor);
- this.field_0.fillRect(0, 0, this.width - this.cellW - 2 * this.GAP, this.p1height);
- this.field_0.setGrayScale(this.pencolor);
- this.field_0.drawString("T: " + temp + " S: " + this.scores + " L:" + level, 1, 1, 16 | 4);
- }
-
- private void drawWaitblock(int val) {
- this.field_0.drawImage(this.imgs[val - 1], this.width - this.cellW - 2 * this.GAP, 2, 16 | 4);
- }
-
- private void moveBlock(int from_x, int from_y, int to_x, int to_y, int num) {
- this.clearBlock(from_x, from_y);
- int x = this.getLeft(to_x);
- int y = this.getTop(to_y);
- this.board[to_y][to_x] = num;
- int ii = this.board[to_y][to_x] - 1;
- this.field_0.drawImage(this.imgs[ii], x, y, 16 | 4);
- }
-
- private void findconnect(int cx, int cy, int value) {
- this.board[cy][cx] = value * -1;
- if (cx < 6 && cy >= 0 && cy <= 7 && this.board[cy][cx + 1] == value) {
- ++count;
- this.findconnect(cx + 1, cy, value);
- }
-
- if (cx > 0 && cy >= 0 && cy <= 7 && this.board[cy][cx - 1] == value) {
- ++count;
- this.findconnect(cx - 1, cy, value);
- }
-
- if (cx >= 0 && cx <= 6 && cy < 6 && this.board[cy + 1][cx] == value) {
- ++count;
- this.findconnect(cx, cy + 1, value);
- }
-
- if (cx >= 0 && cx <= 6 && cy > 0 && this.board[cy - 1][cx] == value) {
- ++count;
- this.findconnect(cx, cy - 1, value);
- }
-
- }
-
- private void reverse() {
- for(int i = 0; i < 7; ++i) {
- for(int j = 0; j < 7; ++j) {
- if (this.board[i][j] < 0) {
- this.board[i][j] *= -1;
- }
- }
- }
-
- }
-
- private void remove() {
- for(int i = 0; i < 7; ++i) {
- for(int j = 0; j < 7; ++j) {
- if (this.board[i][j] < 0) {
- this.board[i][j] = 0;
- this.clearBlock(j, i);
- --this.remains;
- }
- }
- }
-
- }
-
- private void rearrange() {
- boolean b = true;
-
- while(b) {
- b = false;
-
- for(int i = 5; i >= 1; --i) {
- for(int j = 0; j < 7; ++j) {
- if (this.board[i + 1][j] == 0 && this.board[i][j] != 0) {
- this.moveBlock(j, i, j, i + 1, this.board[i][j]);
- b = true;
- }
- }
- }
- }
-
- }
-
- private void clearBlock(int xpos, int ypos) {
- int x = this.getLeft(xpos);
- int y = this.getTop(ypos);
- this.board[ypos][xpos] = 0;
- this.field_0.setGrayScale(this.bgcolor);
- this.field_0.fillRect(x, y, this.bsize, this.bsize);
- }
-
- private void raiseBlocks() {
- int temp = 1;
-
- for(int i = 1; i < 7; ++i) {
- for(int j = 0; j < 7; ++j) {
- if (this.board[i][j] != 0) {
- this.moveBlock(j, i, j, i - 1, this.board[i][j]);
- }
- }
- }
-
- for(int j = 0; j < 7; ++j) {
- boolean cont = true;
-
- while(cont) {
- cont = false;
- temp = (this.random.nextInt() >>> 1) % 6 + 1;
- if (j != 0) {
- if (temp == this.board[6][j - 1] || temp == this.board[5][j]) {
- cont = true;
- }
- } else if (temp == this.board[5][j]) {
- cont = true;
- }
- }
-
- this.board[6][j] = temp;
- int x = this.getLeft(j);
- int y = this.getTop(6);
- this.field_0.drawImage(this.imgs[temp - 1], x, y, 16 | 4);
- }
-
- this.remains += 7;
- }
-
- private boolean isGameover() {
- return this.gameOver;
- }
-
- private void gameover() {
- this.field_0.setGrayScale(this.bgcolor);
- this.field_0.fillRect(this.width / 2 - this.fontW2 / 2, this.height / 2, this.fontW2, this.fontH);
- this.field_0.setGrayScale(this.pencolor);
- this.field_0.drawString("Game Over", this.width / 2, this.height / 2, 16 | 1);
- }
-
- private void youwin() {
- this.field_0.setGrayScale(this.bgcolor);
- this.field_0.fillRect(this.width / 2 - this.fontW2 / 2, this.height / 2, this.fontW2, this.fontH);
- this.field_0.setGrayScale(this.pencolor);
- this.field_0.drawString("You Win", this.width / 2, this.height / 2, 16 | 1);
- }
-
- public void endGame() {
- this.runner = null;
- this.gameOver = true;
- }
-
- void restartGame() {
- this.timeleft = this.initTime;
- if (this.state == 5) {
- ++level;
- } else {
- level = 1;
- }
-
- this.extraTime = this.elapsedTime = count = this.scores = 0;
- this.field_0.setGrayScale(this.bgcolor);
- this.field_0.fillRect(0, 0, this.width, this.height);
- this.reset();
- if (this.gameOver) {
- this.gameOver = false;
- this.start();
- }
-
- this.gameOver = false;
- }
-
- synchronized void start() {
- this.runner = new Thread(this);
- this.runner.start();
- }
-
- synchronized void pause() {
- }
-
- void dest() {
- this.parent.destroyApp(true);
- }
-
- public void keyRepeated(int keyCode) {
- this.keyPressed(keyCode);
- }
-
- public void keyPressed(int keyCode) {
- if (this.direct == 0) {
- int action = ((Canvas)this).getGameAction(keyCode);
- switch (action) {
- case 2:
- this.direct = 1;
- case 3:
- case 4:
- default:
- break;
- case 5:
- this.direct = 3;
- break;
- case 6:
- this.direct = 2;
- }
- }
-
- }
- }
-